using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SolarWinds.Widgets;

namespace SolarWinds.Screens
{
    public class OptionsScreen : Screen
    {
        private readonly WidgetManager mWidgetManager;
        private GraphicsDeviceManager mGraphicsDeviceManager;
        private IScreenManagerService mScreenManager;

        Window mWindow;
        PushButton mCancelButton;
        PushButton mOkButton;
        LabelSpinBox mResolutionSpinBox;
        CheckBox mFullScreenCheckBox;
        private TextBox mPlayernameTextBox;

        public OptionsScreen(Game game)
            : base(game)
        {
            mWidgetManager = new WidgetManager(game);
            Components.Add(mWidgetManager);

            BlocksUpdate = true;
            ColorReduction = 0.2f;
        }

        public override void  RemoveEventHandlers()
        {
            mCancelButton.Clicked -= CancelButtonClicked;
            mOkButton.Clicked -= OkButtonClicked;
        }

        public override void Initialize()
        {
            mScreenManager = (IScreenManagerService)
                             Game.Services.GetService(typeof(IScreenManagerService));

            mGraphicsDeviceManager = (GraphicsDeviceManager)
                                     Game.Services.GetService(typeof(IGraphicsDeviceService));

            base.Initialize();

            #region Widgets

            mWindow = new Window(Game)
                          {
                              Position = new Point(100, 100),
                              Caption = "Game Options",
                              Width = 600,
                              Height = 400,
                              Moveable = false,
                              Resizeable = false,
                              HasCloseButton = false,
                              Tabable = false
                          };
            mWidgetManager.AddChild(mWindow);

            mCancelButton = new PushButton(Game)
                                {
                                    Position = new Point(360, 365),
                                    Width = 100,
                                    Height = 16,
                                    Caption = "Cancel"
                                };
            mCancelButton.Clicked += CancelButtonClicked;

            mOkButton = new PushButton(Game)
                            {
                                Position = new Point(470, 365), 
                                Width = 100, 
                                Height = 16, 
                                Caption = "Ok"
                            };
            mOkButton.Clicked += OkButtonClicked;

            var resLabel = new Label(Game)
                               {
                                   Caption = "Resolution",
                                   Position = new Point(50, 50),
                                   Height = 16,
                                   Width = 150,
                                   Tabable = false
                               };
            mWindow.AddChild(resLabel);

            mResolutionSpinBox = new LabelSpinBox(Game)
                                     {
                                         PossibleValues = new[] {"640x480", "800x600", "1024x768"},
                                         Position = new Point(150, 50),
                                         Height = 16,
                                         Width = 150
                                     };

            mFullScreenCheckBox = new CheckBox(Game)
                                      {
                                          Caption = "Fullscreen",
                                          Position = new Point(50, 80),
                                          Width = 300,
                                          Height = 16
                                      };

            mPlayernameTextBox = new TextBox(Game)
            {
                Position = new Point(50, 110),
                Width = 300,
                Height = 32
            };

            mWindow.AddChild(mCancelButton);
            mWindow.AddChild(mOkButton);
            mWindow.AddChild(mResolutionSpinBox);
            mWindow.AddChild(mFullScreenCheckBox);
            mWindow.AddChild(mPlayernameTextBox);

            mResolutionSpinBox.ValueChanged += ResolutionSpinBoxValueChanged;
            mFullScreenCheckBox.Clicked += FullScreenCheckBoxClicked;

            #endregion

            ReadSettings();
        }

        void OkButtonClicked(object sender, EventArgs e)
        {
            mScreenManager.PopScreen();
            //optionsScreen.Dispose();

            // save new settings
            SaveSettings();
        }

        void CancelButtonClicked(object sender, EventArgs e)
        {
            mScreenManager.PopScreen();
            //optionsScreen.Dispose();

            // restore the old settings
            RestoreSettings();
        }

        void ResolutionSpinBoxValueChanged(object sender, EventArgs e)
        {
            string[] widthHeight = mResolutionSpinBox.CurrentValue.Split('x');

            mGraphicsDeviceManager.PreferredBackBufferWidth = int.Parse(widthHeight[0]);
            mGraphicsDeviceManager.PreferredBackBufferHeight = int.Parse(widthHeight[1]);
            mGraphicsDeviceManager.ApplyChanges();
        }

        void FullScreenCheckBoxClicked(object sender, EventArgs e)
        {
            mGraphicsDeviceManager.IsFullScreen = mFullScreenCheckBox.Checked;
            mGraphicsDeviceManager.ApplyChanges();
        }

        private void ReadSettings()
        {
            Settings settings = Settings.Load("settings.xml");

            mResolutionSpinBox.CurrentValue = settings.ScreenWidth + "x" + settings.ScreenHeight;
            mFullScreenCheckBox.Checked = settings.FullScreen;
        }

        private void RestoreSettings()
        {
            Settings settings = Settings.Load("settings.xml");

            mResolutionSpinBox.CurrentValue = settings.ScreenWidth + "x" + settings.ScreenHeight;
            mFullScreenCheckBox.Checked = settings.FullScreen;

            mGraphicsDeviceManager.PreferredBackBufferWidth = settings.ScreenWidth;
            mGraphicsDeviceManager.PreferredBackBufferHeight = settings.ScreenHeight;
            mGraphicsDeviceManager.IsFullScreen = settings.FullScreen;
            mGraphicsDeviceManager.ApplyChanges();
        }

        private void SaveSettings()
        {
            Settings settings = Settings.Load("settings.xml");

            string[] widthHeight = mResolutionSpinBox.CurrentValue.Split('x');

            settings.ScreenWidth = int.Parse(widthHeight[0]);
            settings.ScreenHeight = int.Parse(widthHeight[1]);
            settings.FullScreen = mFullScreenCheckBox.Checked;

            settings.Save("settings.xml");
        }
    }
}